/**
 * Created by Administrator on 2017/3/1 0001.
 */
var ratio=window.devicePixelRatio;
var pixiStageW=740;
var pixiStageH=1136;
var renderer=PIXI.autoDetectRenderer(pixiStageW,pixiStageH,{antialias:true,transparent:true,resolution:1});
var stage=new PIXI.Stage(0xFF0000);
var resource;
var resourcePre;

var div_pixi=document.createElement('div');
document.body.appendChild(div_pixi);
div_pixi.appendChild(renderer.view);

var preload=Preload.create();
preload.init(pixiStageW,pixiStageH);
stage.addChild(preload.ui);


document.addEventListener('Main_LoadCompletePre',preLoadComplete);
document.addEventListener('Main_LoadComplete',loadComplete);
document.addEventListener('Main_RemovePreLoad',removePreLoad);
// document.addEventListener('Game_ScrollComplete',scrollCompleteHander);
// document.addEventListener('Game_ScrollStart',scrollStartHander);

function preLoadComplete(e) {
    resourcePre=e.detail.resources;
}
function loadComplete(e) {
    resource=e.detail.resources;
    gameIn();
}
function removePreLoad() {
    stage.removeChild(preload.ui);
}
var tw_music;
function gameIn() {
    var game=Game.create();
    // stage.addChildAt(game.ui,0);
    var sp_music=new PIXI.Sprite(resource['assets/images/music.png'].texture);
    sp_music.anchor.set(0.5)
    sp_music.x=665;
    sp_music.y=47;
    sp_music.alpha=0.9;
    stage.addChild(sp_music);
    tw_music=new TWEEN.Tween({rot:0})
        .to({rot:Math.PI*2})
        .onUpdate(function () {
            sp_music.rotation=this.rot;
        })
        .repeat(Infinity)
        .start();
    sp_music.interactive=true;
    sp_music.on('pointerdown',function () {
        if(sp_music.alpha==0.9)
        {
            sp_music.alpha=1
            tw_music.stop();
            TWEEN.remove(tw_music)
            sp_music.rotation=0;
            volumeChange(0);
        }
        else
        {
            tw_music=new TWEEN.Tween({rot:0})
                .to({rot:Math.PI*2})
                .onUpdate(function () {
                    sp_music.rotation=this.rot;
                })
                .repeat(Infinity)
                .start();
            sp_music.alpha=0.9;
            tw_music.start();
            volumeChange(1);
        }
    })

}

resizeHandler();
window.onresize = resizeHandler;
function resizeHandler() {
    renderer.resize(window.innerWidth, window.innerHeight);
    mode='noBorder';
    switch (mode) {
        case 'exactFit':
            stage.scale.x = window.innerWidth / pixiStageW;
            stage.scale.y = window.innerHeight / pixiStageH;
            break;
        case 'noBorder':
            stage.scale.x = (window.innerHeight / pixiStageH < window.innerWidth / pixiStageW) ? (window.innerWidth / pixiStageW) : (window.innerHeight / pixiStageH);
            stage.scale.y = stage.scale.x;
            break;
        case 'noScale':
            stage.scale.x = 1;
            stage.scale.y = 1;
            break;
        case 'showAll':
            stage.scale.x = (window.innerHeight / pixiStageH < window.innerWidth / pixiStageW) ? (window.innerHeight / pixiStageH) : (window.innerWidth / pixiStageW);
            stage.scale.y = stage.scale.x;
            break;
    }
    stage.x = (window.innerWidth - pixiStageW * stage.scale.x) * .5;
    stage.y = (window.innerHeight - pixiStageH * stage.scale.y) * .5;
};

requestAnimationFrame(animate);
function animate(time) {
    requestAnimationFrame(animate);
    renderer.render(stage);
    TWEEN.update(time);
}